package com.fjec.market.schedule.handler;

import com.fjec.common.util.DateUtil;
import com.fjec.common.util.JsonUtil;
import com.fjec.common.ys.PaidHelper;
import com.fjec.common.ys.protocol.ResRegister;
import com.fjec.common.ys.protocol.ResSearchReg;
import com.fjec.common.ys.protocol.YsConst;
import com.fjec.market.entity.BizYespayRegLog;
import com.fjec.market.entity.InfoYsepayRegister;
import com.fjec.market.service.IBizYespayRegLogService;
import com.fjec.market.service.IInfoYsepayRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class YsRegResult99Handler {

    @Resource
    private IInfoYsepayRegisterService ysepayRegisterService;

    @Resource
    private PaidHelper paidHelper;

    @Resource
    private IBizYespayRegLogService bizYespayRegLogService;

    public String handle(int limit, int minutes) {
        int num = 0;
        // 查询RegStatus为99的数据进行处理
        List<InfoYsepayRegister> entityList = ysepayRegisterService.getEntityByRegStatus(99, limit, minutes);
        for (InfoYsepayRegister entity : entityList) {
            boolean success = false;
            // 获取MerchantNo范围，进行查询进件结果
            StringBuilder sb = new StringBuilder();
            sb.append(entity.getBusiType())
                    .append("-")
                    .append(entity.getBusiId())
                    .append("-");
            try {
                Integer updateVer = entity.getUpdateVer();
                for (int i = 1; i <= updateVer; i++) {
                    sb.append(i);
                    String strResult = paidHelper.searchRegister(sb.toString());
                    ResSearchReg resSearchReg = JsonUtil.jsonToObject(strResult, ResSearchReg.class);
                    if (resSearchReg.getCode() == 10000) {
                        // 访问成功
                        if (StringUtils.equals(resSearchReg.getUserStatus(), YsConst.YS_REG_SUCCESS_USER_STATUS)) {
                            // 进件生效
                            entity.setRegStatus(3);
                            entity.setMerchantNo1(sb.toString());
                            entity.setRegTime(DateUtil.nowLdt());
                            entity.setUserCode(resSearchReg.getUserCode());
                            entity.setCustId(resSearchReg.getCustId());
                            entity.setUserStatus(resSearchReg.getUserStatus());
                            entity.setRegResult(resSearchReg);
                            success = true;
                            // 写操作日志
                            recordLog(entity, resSearchReg);
                            break;
                        }
                    }
                }
                if (!success) {
                    entity.setRegStatus(4);
                }
                entity.setUpdateTime(DateUtil.nowLdt());
                ysepayRegisterService.updateById(entity);
                num++;
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        return "Handle ys online reg number: " + num;
    }

    private void recordLog(InfoYsepayRegister entity, ResSearchReg resSearchReg) {
        // 银盛进件log表
        BizYespayRegLog logEntity = new BizYespayRegLog();
        logEntity.setRegId(entity.getId());
        logEntity.setCustName(entity.getCustName());
        logEntity.setMerchantNo(entity.getMerchantNo1());
        logEntity.setCreateTime(DateUtil.nowLdt());
        logEntity.setOptType(3);
        if (resSearchReg != null) {
            logEntity.setReturnData(resSearchReg);
            if (resSearchReg.getCode() == 10000) {
                logEntity.setUserCode(resSearchReg.getUserCode());
                logEntity.setCustId(resSearchReg.getCustId());
                logEntity.setUserStatus(resSearchReg.getUserStatus());
                logEntity.setRegSuccess(1);
            } else {
                logEntity.setRegSuccess(0);
            }
        }
        bizYespayRegLogService.save(logEntity);
    }
}
